package com.sjj.springcloud.controller;

import com.sjj.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptConsumerController {//消费者不应该有service层
    //RestTemplate...我们调用前,先注册到spring容器
    //restTemplate.getForObject(url,实体:Map,Class<T>responseType)
    @Autowired
    private RestTemplate restTemplate;
    //private static final String REST_URL="http://localhost:8002/";
    private static final String REST_URL="http://SPRINGCLOUD-PROVIDER-DEPT/";

    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return restTemplate.getForObject(REST_URL+"dept/get/"+id,Dept.class);//此时id作为实体
    }

    @RequestMapping(value = "/consumer/dept/add",produces = "application/json")
    public boolean add(Dept dept){
        return restTemplate.postForObject(REST_URL+"dept/add",dept,boolean.class);
    }

    @RequestMapping("/consumer/dept/getAll")
    public List<Dept> getAll(){
        return restTemplate.getForObject(REST_URL+"dept/getAll",List.class);//此时id作为实体
    }
}
